package com.ruoyi.web.controller.visit;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.enterprise.domain.EnterpriseArchive;
import com.ruoyi.enterprise.service.IEnterpriseArchiveService;
import com.ruoyi.visit.domain.WithinManaged;
import com.ruoyi.visit.service.IWithinManagedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 限期整改Controller
 * 
 * @author ruoyi
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/visit/managed")
public class WithinManagedController extends BaseController
{
    @Autowired
    private IWithinManagedService withinManagedService;

    @Autowired
    private IEnterpriseArchiveService enterpriseArchiveService;

    /**
     * 查询限期整改列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WithinManaged withinManaged)
    {
        startPage();
        List<WithinManaged> list = withinManagedService.selectWithinManagedList(withinManaged);
        for(WithinManaged managed:list){
            String unitEpId = managed.getWmUnit();
            System.out.println("=========================================="+unitEpId);
            EnterpriseArchive enterpriseArchive = new EnterpriseArchive();
            enterpriseArchive.setEpId(Long.getLong(unitEpId));
           List<EnterpriseArchive> epList =  enterpriseArchiveService.selectEnterpriseArchiveList(enterpriseArchive);
           managed.setWmUnit(epList.get(0).getEpName());
        }
        return getDataTable(list);
    }

    /**
     * 导出限期整改列表
     */
    @Log(title = "限期整改", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WithinManaged withinManaged)
    {
        List<WithinManaged> list = withinManagedService.selectWithinManagedList(withinManaged);
        ExcelUtil<WithinManaged> util = new ExcelUtil<WithinManaged>(WithinManaged.class);
        return util.exportExcel(list, "限期整改数据");
    }

    /**
     * 获取限期整改详细信息
     */
    @GetMapping(value = "/{wmId}")
    public AjaxResult getInfo(@PathVariable("wmId") Long wmId)
    {
        return AjaxResult.success(withinManagedService.selectWithinManagedById(wmId));
    }

    /**
     * 新增限期整改
     */
    @Log(title = "限期整改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WithinManaged withinManaged)
    {
        return toAjax(withinManagedService.insertWithinManaged(withinManaged));
    }

    /**
     * 修改限期整改
     */
    @Log(title = "限期整改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WithinManaged withinManaged)
    {
        return toAjax(withinManagedService.updateWithinManaged(withinManaged));
    }

    /**
     * 删除限期整改
     */
    @Log(title = "限期整改", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wmIds}")
    public AjaxResult remove(@PathVariable Long[] wmIds)
    {
        return toAjax(withinManagedService.deleteWithinManagedByIds(wmIds));
    }
}
